/*
 * ioevent.c
 *
 *  Created on: 2012-3-15
 *      Author: wutao
 */

#include "ioevent.h"
#include "msgqueue.h"
#include "util.h"
#include <memory.h>
#include <stdio.h>


int decodec(AppSession *session, struct buffer *buff) {
	while (1) {
		u_int32_t length_readable = buff->limit - buff->index;
		if (length_readable < 1) {
			break;
		}

		u_int32_t mark = buff->index;
		int count = 0;
		int i = mark;
		for (; count < length_readable; i++, count++) {
			if (buff->data[i] == '\n') {
				break;
			}
		}
		if(count==length_readable){
			break;
		}

		char *msg = (char *)malloc((size_t)(count+1));
		char *bytes_begin = buff->data + buff->index;
		memcpy(msg, bytes_begin, count);
		buff->index = buff->index + count+2;
		appendToTail(session->requests,NULL);
//		addmsg_4reading(session, msg);
	}
	return 0;
}

int encodec(AppSession *session, void *msg) {
	return 0;
}

void newconn(AppSession *session, struct sockaddr *addr) {
	return;
}
void closed(AppSession *session) {
	return;
}
void requested(AppSession *session, void *msg) {
	if(is_closed(session)){
		free((void *)msg);
		return ;
	}
	printf("rcv msg %s:\n",(char*)msg);
	free((void *)msg);
	return;
}
void responsed(AppSession *session, void *msg) {
	free((void *)msg);
	return;
}
void errored(AppSession *session, void *err) {
	return;
}
